package com.zmn.oms.zmn.business.impl.work.list;

import com.google.common.collect.Lists;
import com.zmn.brs.common.enums.EventEnum;
import com.zmn.brs.common.enums.RecordFieldEnum;
import com.zmn.brs.starter.annotation.RecordField;
import com.zmn.brs.starter.annotation.StaffOperateRecord;
import com.zmn.oms.business.interfaces.work.OrderWorkListBService;
import com.zmn.oms.common.enums.EncryptFieldTypeEnum;
import com.zmn.oms.common.utils.MobileUtil;
import com.zmn.oms.model.entity.work.OrderWorkQuery;
import com.zmn.base.oms.common.model.es.orderwork.EsOrderWork;
import com.zmn.base.oms.common.model.es.orderwork.EsOrderWorkContact;
import com.zmn.oms.model.vo.warning.ZsWarningVO;
import com.zmn.oms.model.vo.work.list.ZsOrderWorkQualityControlVO;
import com.zmn.oms.services.interfaces.orderencrypt.OrderEncryptService;
import com.zmn.oms.zmn.business.interfaces.work.ZsOrderWorkQueryBService;
import com.zmn.oms.zmn.business.interfaces.work.list.ZsOrderWorkQualityControlQueryBService;
import com.zmn.pay.common.constant.PayConsts;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 类描述：品控工单查询
 *
 * @author lujia
 * @date 2018/12/27 11:35
 */
@Service
public class ZsOrderWorkQualityControlQueryBServiceImpl implements ZsOrderWorkQualityControlQueryBService {

    @Autowired
    OrderWorkListBService orderWorkListBService;
    @Autowired
    ZsOrderWorkQueryBService zsOrderWorkQueryBService;
    @Autowired
    OrderEncryptService orderEncryptService;

    @StaffOperateRecord(staffId = "{{#query.staffId}}",
            event = EventEnum.WORK_ORDER_QUERY,
            mapping = {
            @RecordField(field = RecordFieldEnum.STAFF_IP, value = "{{#query.ip}}"),
            @RecordField(field = RecordFieldEnum.PLAT, value = "{{#query.platWork}}")
    })
    @Override
    public List<ZsOrderWorkQualityControlVO> listPageOrderWorkByQuery(OrderWorkQuery query) {
        return listPageOrderWorkByQueryNoRecord(query);
    }

    @Override
    public List<ZsOrderWorkQualityControlVO> listPageOrderWorkByQueryNoRecord(OrderWorkQuery query) {

        zsOrderWorkQueryBService.processWorkQuery(query);

        // 查询
        List<EsOrderWork> esOrderWorks = orderWorkListBService.listPageOrderWorkByQuery(query);
        // 解密电话和地址
        orderEncryptService.batchDecryptEsOrderWork(esOrderWorks,EncryptFieldTypeEnum.USER_TELPHONE,EncryptFieldTypeEnum.USER_ADDRESS);

        // 警告
        Map<Long, ZsWarningVO> warningMap = zsOrderWorkQueryBService.listWarningByWorkList(esOrderWorks);

        // 转 vo
        List<ZsOrderWorkQualityControlVO> voList = Lists.newArrayList();
        esOrderWorks.forEach((esOrderWork -> {
            ZsOrderWorkQualityControlVO vo = new ZsOrderWorkQualityControlVO();
            BeanUtils.copyProperties(esOrderWork, vo);
            vo.setWorkId(esOrderWork.getId());
            vo.setPrepayStatusName(PayConsts.getPayStatusName(esOrderWork.getPrepayStatus()));
            vo.setDepositStatusName(PayConsts.getPayStatusName(esOrderWork.getDepositStatus()));
            EsOrderWorkContact concat = esOrderWork.getContact();
            vo.setUsername(concat.getContactName());
            if (query.getIsSensitiveAddr()) {
                vo.setUserAddress("****");
            } else {
                vo.setUserAddress(concat.getAddress());
            }
            vo.setUserStreet(concat.getStreet());
            vo.setGender(concat.getGender());
            if (query.getIsSensitiveAddr()) {
                vo.setUserPhone(MobileUtil.formatHideTel(concat.getPhone()[0]));
            } else {
                vo.setUserPhone(concat.getPhone()[0]);
            }
            if (warningMap != null) {
                vo.setWarningVO(warningMap.get(esOrderWork.getId()));
            }

            voList.add(vo);
        }));
        return voList;
    }
}
